package com.demo.producer.controller;

import com.alibaba.fastjson.JSON;

import com.demo.producer.entity.MapEntity;
import com.demo.producer.entity.MyMessage;
import com.demo.producer.entity.RsmEntity;
import com.demo.producer.job.process.TestProcess;
import com.demo.producer.producer.MyGateway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.atomic.AtomicInteger;

@RequestMapping("/mqttTest")
@RestController
public class MqttTestController {
    public static AtomicInteger index = new AtomicInteger(0);

    @Resource
    private MyGateway myGateway;

    @Autowired
    TestProcess testProcess;

    @RequestMapping("/send")
    public String send(@RequestBody MyMessage myMessage) throws Exception{

        for (int i = 0; i < 20; i++) {
            new Thread(() -> {
                for (int j = 0; j < 10; j++) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    this.myGateway.sendToMqtt(myMessage.getTopic(), 1, myMessage.getContent() + "-" + j);
                }
            }).start();
        }


        return "send topic" + myMessage.getTopic() + " ,message : " + myMessage.getContent();
    }

    /**
     * 模拟平台下发ota消息
     * @return
     */
    @GetMapping("/sendOta")
    public String sendOta(@RequestParam String jobName) {
        if ("testJob".equals(jobName)) {
            testProcess.process();
        }
        return "正在发送";
    }

    /**
     * 模拟平台下发map消息
     * @return
     */
    @RequestMapping("/sendMap")
    public String sendMap() {

        new Thread(() -> {
            for (int j = 0; j < 2000; j++) {
                //String topic = "V2X/UP/D/rsuZwh000" + (j % 10);
                //String topic = "V2X/UP/D/rsuZwh000";
                //String topic = "WELLNESS/UP/A";
                //String topic = "V2X/UP/D/0001";
                //String topic = "V2X/DOWN/D/0001";
                String topic = "V2X/DOWN/D/0001";

                MapEntity mapData = new MapEntity();
                mapData.setDid(mapData.getDid());
                mapData.setMsgFrame(mapData.getMsgFrame());
                mapData.setProtocolVersion(mapData.getProtocolVersion());
                mapData.setLength(mapData.getLength());
                mapData.setTimestamp(mapData.getTimestamp());
                mapData.setSeqNum(mapData.getSeqNum());
                mapData.setAck(mapData.getAck());
                mapData.setMapDatas(mapData.getMapDatas());

                this.myGateway.sendToMqtt(topic, 1, JSON.toJSONString(mapData));

                System.out.println("TOPIC:" + topic + "数据已下发" + JSON.toJSONString(mapData));
                try {
                    //Thread.sleep(5);
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        return "正在发送";
    }

    /**
     * 模拟平台下发rsm消息
     * @return
     */
    @RequestMapping("/sendRsm")
    public String sendRsm() {

        new Thread(() -> {
            for (int j = 0; j < 2000; j++) {
                //String topic = "V2X/UP/D/rsuZwh000" + (j % 10);
                //String topic = "V2X/UP/D/rsuZwh000";
                //String topic = "WELLNESS/UP/A";
                //String topic = "V2X/UP/D/0001";
                //String topic = "V2X/DOWN/D/0001";
                String topic = "V2X/DOWN/D/0001";

                RsmEntity rsmEntity = new RsmEntity();
                rsmEntity.setDId(rsmEntity.getDId());
                rsmEntity.setMsgFrame(rsmEntity.getMsgFrame());
                rsmEntity.setProtocolVersion(rsmEntity.getProtocolVersion());
                rsmEntity.setLength(rsmEntity.getLength());
                rsmEntity.setRsmDatas(rsmEntity.getRsmDatas());
                rsmEntity.setTimestamp(rsmEntity.getTimestamp());
                rsmEntity.setAck(rsmEntity.getAck());
                rsmEntity.setSeqNum(rsmEntity.getSeqNum());
                this.myGateway.sendToMqtt(topic, 1, JSON.toJSONString(rsmEntity));

                System.out.println("TOPIC:" + topic + "数据已下发" + JSON.toJSONString(rsmEntity));
                try {
                    //Thread.sleep(5);
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        return "正在发送";
    }
}
